package com.platform.enums;

import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IDictEntry;
import com.platform.comm.enums.IEnumItem;
import com.platform.comm.enums.IEnumOption;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Title: DirExTransmitKind
 * Description: 资源交换传输方式
 * Date 2020/4/20
 *
 * @author wxw
 * @version v1.0
 */
@EnumOption(name = "transmitKind")
public enum DirExTransmitKind implements IDictEntry, IEnumItem, IEnumOption<String> {
    /**
     * 交换媒介
     */
    FILE("上传文件"), FTP("文件传输"), DB_DB("库对库"), API("接口"), SG("手工录入");

    public final String title;

    DirExTransmitKind(String title) {
        this.title = title;
    }

    /**
     * 根据枚举"键"值解析枚举值
     *
     * @param code 枚举"键"值
     * @return DirExTransmitKind
     */
    public static DirExTransmitKind parse(String code) {
        try {
            return valueOf(code);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据枚举"键"值解析枚举值，如果未找到匹配项，则返回默认值
     *
     * @param code 枚举"键"值
     * @param def  默认值
     * @return DirExTransmitKind
     */
    public static DirExTransmitKind parse(String code, DirExTransmitKind def) {
        try {
            return valueOf(code);
        } catch (Exception e) {
            return def;
        }
    }


    @Override
    public String getKey() {
        return name();
    }

    @Override
    public String getValue() {
        return title;
    }

    @Override
    public Object getData() {
        return this;
    }

    @Override
    public boolean is(String code) {
        return name().equals(code);
    }

    /**
     * 枚举项 “键”
     *
     * @return key
     */
    @Override
    public String key() {
        return name();
    }

    /**
     * 枚举项 “值”
     *
     * @return value
     */
    @Override
    public String value() {
        return title;
    }

    /**
     * 枚举options
     *
     * @return
     */
    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }
}
